package cn.hu.org.controller;

import cn.hu.basic.annotation.PreAuthorize;
import cn.hu.basic.util.AjaxResult;
import cn.hu.basic.util.PageList;
import cn.hu.org.domain.Department;
import cn.hu.org.query.DepartmentQuery;
import cn.hu.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    @GetMapping("/{id}")
    public Department findOne(@PathVariable Long id){
        return departmentService.findById(id);
    }

    @GetMapping
    public List<Department> findAll(){
        return departmentService.findAll();
    }

    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Department department){
            if(department.getId() == null){
                departmentService.add(department);
            }else {
                departmentService.update(department);
            }
            return AjaxResult.getInstance();
    }

    @PostMapping
    public PageList<Department> getListByQuery(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.listByQuery(departmentQuery);
    }

    @DeleteMapping("/{id}")
    @PreAuthorize(name="通过id删除部门",value="dept:deleteById")
    public AjaxResult deleteDep(@PathVariable Long id){
            departmentService.delete(id);
            return AjaxResult.getInstance();
    }

    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
            departmentService.patchDelete(ids);
            return AjaxResult.getInstance();
    }

    @GetMapping("/tree")
    public List<Department> getDeptTree(){
        return departmentService.getDeptTree();
    }
}
